﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

/*
 * Opisuje prymityw przedstawiający kwadrat.
 */
namespace Projekt.Models.Primitives
{
    public abstract class Primitive : DrawableGameComponent
    {
        protected Engine game;
        protected GraphicsDevice graphicsDevice;
        protected BasicEffect basicEffect;
        protected Matrix Transforms;
        protected List<VertexPositionColor[]> vertices;
        
        private Vector3 position;
        public Vector3 Position
        {
            get { return position; }
            set
            {
                position = value;
                Transform();
            }
        }
        private Vector3 rotation;
        public Vector3 Rotation
        {
            get { return rotation; }
            set
            {
                rotation = value;
                Transform();
            }
        }
        private Vector3 scale;
        public Vector3 Scale
        {
            get { return scale; }
            set
            {
                scale = value;
                Transform();
            }
        }

        public Primitive(GraphicsDevice graphicsDevice, Engine game)
            : base(game)
        {
            this.graphicsDevice = graphicsDevice;
            this.game = game;

            this.position = Vector3.Zero;
            this.rotation = Vector3.Zero;
            this.scale = new Vector3(1);

            basicEffect = new BasicEffect(graphicsDevice);
            basicEffect.VertexColorEnabled = true;

            this.vertices = new List<VertexPositionColor[]>();

            this.Visible = false;
        }

        public override void Draw(GameTime gameTime)
        {
            basicEffect.Projection = game.camera.Projection;
            basicEffect.View = game.camera.View;
            basicEffect.World = Transforms;

            basicEffect.CurrentTechnique.Passes[0].Apply();

            foreach (VertexPositionColor[] tab in vertices)
            {
                graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, tab, 0, tab.Length - 1);
            }            
            
            base.Draw(gameTime);
        }

        public void Transform()
        {
            Transforms = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position);
        }

        public override string ToString()
        {
            return "primitive";
        }
    }
}
